﻿/************************************************************************

 This file is part of EscapeFromPrisonPlanet.

 EscapeFromPrisonPlanet is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 EscapeFromPrisonPlanet is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2012 Scott Bevin. all rights reserved

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare25Project.GameScreens
{
    internal class WhiteFadeScreen : GameScreen
    {
        private Texture2D WhiteTex;

        public enum States
        {
            FadingIn,
            FadeInPause,
            FadedIn,
            FadingOut,
        }

        public States State = States.FadingIn;

        public float FadeInTime = 1.5f;
        public float PauseTime = 0.5f;
        public float FadeOutTime = 1.5f;
        private float Timer = 0.0f;

        public WhiteFadeScreen()
            : base(false)
        {
        }

        public void FadeOut()
        {
            Timer = 0.0f;

            if (State == States.FadingIn)
            {
                Timer = FadeOutTime * (Timer / FadeInTime);
            }

            State = States.FadingOut;
        }

        public override void OnAdded()
        {
            WhiteTex = ContentManager.Load<Texture2D>("Img/White");

            base.OnAdded();
        }

        public override void Render(float dt, GraphicsDevice gd, SpriteBatch sprites)
        {
            float alpha = 0.0f;

            if (State == States.FadingIn)
            {
                Timer += dt;
                alpha = Timer / FadeInTime;

                if (Timer >= FadeInTime)
                {
                    State = States.FadeInPause;
                    Timer = 0.0f;
                }
            }
            else if (State == States.FadeInPause)
            {
                Timer += dt;
                if (Timer >= PauseTime)
                {
                    State = States.FadedIn;
                }

                alpha = 1.0f;
            }
            else if (State == States.FadedIn)
            {
                alpha = 1.0f;
            }
            else if (State == States.FadingOut)
            {
                Timer += dt;

                if (Timer >= FadeInTime)
                {
                    this.KillScreen = true;
                }

                alpha = 1.0f - (Timer / FadeOutTime);
            }

            sprites.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, DepthStencilState.Default,
                RasterizerState.CullCounterClockwise);

            sprites.Draw(WhiteTex,
               new Microsoft.Xna.Framework.Rectangle(0, 0, gd.PresentationParameters.BackBufferWidth, gd.PresentationParameters.BackBufferHeight),
               Color.White * alpha);

            sprites.End();

            base.Render(dt, gd, sprites);
        }
    }
}